<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
class SafeController extends Controller
{
    public function index()
    {
        return view('/web/Member/Member_Safe');
    }

    public function phone(Request $request)
    {
        $id = session('uid');
        $arr = DB::table('user_create')->where('id',$id)->first();
        $oldPhone = $request->all();
        $phone = $request->except('_token','oldPhone');
        // var_dump($phone);die;
        if($oldPhone['oldPhone'] != $arr['phone']){

            return back()->with('phoneError','原手机号不正确，请输入正确的手机号进行更改！！！');
        }

        $this->validate($request,[
            'phone'=>'required|regex:/^1[34578]\d{9}$/',
            ],[
            
            'phone.required' => '修改密码不能为空！！！',
            'phone.regex' => '请输入正确的手机号！！！',
            ]);
        
        $res = DB::table('user_create')->where('id',$id)->update($phone);
        if($res) {

            return redirect('/safe/index')->with('phoneSuccess','绑定手机号修改成功!!!');
        }
    }

    public function email(Request $request)
    {
       
        $arr = DB::table('user_create')->where('id',session('uid'))->first();
        $oldEmail = $request->all();
        $email = $request->except('_token','oldEmail');
        // var_dump($phone);die;
        if($oldEmail['oldEmail'] != $arr['email']){

            return back()->with('emailError','原邮箱不正确，请输入正确的邮箱进行更改！！！');
        }


        $this->validate($request,[
            'email'=>'required|email',
            ],[
            'email.required' => '邮箱不能为空！！！',
            'email.email' => '邮箱格式不正确！！！',
        ]);

        $res = DB::table('user_create')->where('id',session('uid'))->update($email);
        if($res) {

            return redirect('/safe/index')->with('emailSuccess','绑定邮箱修改成功!!!');
        }
    }

    public function password(Request $request)
    {

        $arr = DB::table('user_create')->where('id',session('uid'))->first();
        $oldPassword = $request->all();
        $password = $request->except('_token','oldPassword');
        // var_dump($phone);die;
        if(!Hash::check($oldPassword['oldPassword'],$arr['password'])){

            return back()->with('passError','原密码不正确，请输入正确的密码进行更改！！！');
        }

        $this->validate($request,[
            'password'=>'required|regex:/^\S{6,18}$/|same:repassword',
            'repassword' => 'required',
            ],[
            'password.required' => '密码不能为空！！！',
            'password.regex' => '密码格式不正确，请重新输入！！！',
            'password.same' => '确认密码也密码不一致，请确认后提交！！！',
            'repassword.required' => '确认密码不能为空！！！',
        ]);

        $newpass['password'] = Hash::make($password['password']);
        // var_dump($newpass);die;
        $res = DB::table('user_create')->where('id',session('uid'))->update($newpass);
        if($res) {

            session(['uid'=>'','username'=>'']);
            // var_dump(session('uid'));
            // var_dump(session('username'));die;
            return redirect('/login')->with('success','密码修改成功,请重新登录');
        }
    }
}
